package com.moming.controller;

import java.util.Date;
import java.util.Hashtable;
import java.util.List;
import java.util.Map;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.alibaba.fastjson.JSON;
import com.moming.model.CorpUserModel;
import com.moming.model.PWBResponseXml;
import com.moming.model.TciketModel;
import com.moming.request.OrderItemInfoRequest;
import com.moming.request.TicketOrderRequest;
import com.moming.util.HttpUtil;
import com.moming.util.TxtUtil;
import com.moming.util.XmlHelper;
import com.moming.utils.DateUtils;

@Controller
public class IndexController extends BaseController {

	@ResponseBody
	@RequestMapping("/syncOrder")
	public PWBResponseXml index(@ModelAttribute("request") OrderItemInfoRequest request) {
		/**
		 * 组合orderInfo内容 xml格式
		 */
		
		CorpUserModel c = JSON.parseObject(request.getCorpUserJson(),
				CorpUserModel.class);
		StringBuilder sb = new StringBuilder();
		sb.append("<PWBRequest>");
		sb.append("<transactionName>SEND_CODE_REQ</transactionName>");
		sb.append("<header>");
		sb.append("<application>SendCode</application>");
		sb.append("<requestTime>"
				+ DateUtils.detailDateFormat.format(new Date())
				+ "</requestTime>");
		sb.append("</header>");
		sb.append("<identityInfo>");
		sb.append("<corpCode>" + c.getUserCode() + "</corpCode>");
		sb.append("<userName>" + c.getUserName() + "</userName>");
		sb.append("</identityInfo>");
		sb.append("<orderRequest>");
		sb.append("<order>");
		sb.append("<certificateNo>" + request.getLinkIdcard()
				+ "</certificateNo>");
		sb.append("<linkName>" + request.getLinkName() + "</linkName>");
		sb.append("<linkMobile>" + request.getLinkMobile() + "</linkMobile>");
		sb.append("<orderCode>" + request.getOrderNo() + "</orderCode>");
		sb.append("<orderPrice>" + request.getPaySum() + "</orderPrice>");
		sb.append("<payMethod>third_vm</payMethod>");
		// 景区门票
		sb.append("<familyOrders>");
		List<TicketOrderRequest> ticketOrders = request.getTicketList();
		// 一个订单多张订单详情信息
		for (TicketOrderRequest item : ticketOrders) {

			TciketModel t = JSON.parseObject(item.getTicketJson(),
					TciketModel.class);
			sb.append("<familyOrder>");
			sb.append("<orderCode>" + item.getTicketOrderNo() + "</orderCode>");
			sb.append("<price>" + item.getPrice() + "</price>");
			sb.append("<quantity>" + item.getAmount() + "</quantity>");
			sb.append("<totalPrice>" + item.getAmount() * item.getPrice()
					+ "</totalPrice>");
			sb.append("<occDate>" + item.getTravelDate() + "</occDate>");
			sb.append("<goodsCode>" + t.getTicketCode().trim() + "</goodsCode>");
			sb.append("<goodsName>" + t.getTicketName() + "</goodsName>");
			sb.append("</familyOrder>");
		}
		sb.append("</familyOrders>");
		sb.append("</order>");
		sb.append("</orderRequest>");
		sb.append("</PWBRequest>");
		Map<String, String> map = new Hashtable<String, String>();
		String sign = TxtUtil.digest("xmlMsg=" + sb
				+ c.getKey());
		map.put("sign", sign.toLowerCase());
		map.put("xmlMsg", sb.toString());
		String string = HttpUtil.doPost(
				"http://boss.zhiyoubao.com/api/services.htm", map, "utf-8");
		PWBResponseXml respone = XmlHelper.toObj(PWBResponseXml.class, string);
		return respone;
	}
}
